{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TVM Runtime base\n",
    "\n",
    "源码见：`tvm/src/runtime/runtime_base.h`\n",
    "\n",
    "```c++\n",
    "/*!\n",
    " * \\file runtime_base.h\n",
    " * \\brief Base of all C APIs\n",
    " */\n",
    "#ifndef TVM_RUNTIME_RUNTIME_BASE_H_\n",
    "#define TVM_RUNTIME_RUNTIME_BASE_H_\n",
    "\n",
    "#include <tvm/runtime/c_runtime_api.h>\n",
    "\n",
    "#include <stdexcept>\n",
    "\n",
    "/*! \\brief  macro to guard beginning and end section of all functions */\n",
    "#define API_BEGIN() try {\n",
    "/*! \\brief every function starts with API_BEGIN();\n",
    "     and finishes with API_END() or API_END_HANDLE_ERROR */\n",
    "#define API_END()                                         \\\n",
    "  }                                                       \\\n",
    "  catch (::tvm::runtime::EnvErrorAlreadySet & _except_) { \\\n",
    "    return -2;                                            \\\n",
    "  }                                                       \\\n",
    "  catch (std::exception & _except_) {                     \\\n",
    "    return TVMAPIHandleException(_except_);               \\\n",
    "  }                                                       \\\n",
    "  return 0;  // NOLINT(*)\n",
    "/*!\n",
    " * \\brief every function starts with API_BEGIN();\n",
    " *   and finishes with API_END() or API_END_HANDLE_ERROR\n",
    " *   The finally clause contains procedure to cleanup states when an error happens.\n",
    " */\n",
    "#define API_END_HANDLE_ERROR(Finalize)                    \\\n",
    "  }                                                       \\\n",
    "  catch (::tvm::runtime::EnvErrorAlreadySet & _except_) { \\\n",
    "    return -2;                                            \\\n",
    "  }                                                       \\\n",
    "  catch (std::exception & _except_) {                     \\\n",
    "    Finalize;                                             \\\n",
    "    return TVMAPIHandleException(_except_);               \\\n",
    "  }                                                       \\\n",
    "  return 0;  // NOLINT(*)\n",
    "\n",
    "/*!\n",
    " * \\brief handle exception throwed out\n",
    " * \\param e the exception\n",
    " * \\return the return value of API after exception is handled\n",
    " */\n",
    "int TVMAPIHandleException(const std::exception& e);\n",
    "\n",
    "#endif  // TVM_RUNTIME_RUNTIME_BASE_H_\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这段代码是 C++ 头文件，它定义了一些宏和函数，用于处理 TVM 运行时 API 的异常情况。\n",
    "\n",
    "首先，该头文件包含了一些必要的头文件：\n",
    "\n",
    "- `<tvm/runtime/c_runtime_api.h>`：包含 TVM C API 的声明。\n",
    "- `<stdexcept>`：包含标准异常类的定义。\n",
    "\n",
    "接下来，定义了两个宏：\n",
    "1. `API_BEGIN()`：用于标记函数开始执行的位置。在函数体中，使用 `try` 块来捕获可能抛出的异常。\n",
    "2. `API_END()`：用于标记函数结束执行的位置。在函数体中，使用 `catch` 块来捕获特定的异常类型，并返回相应的错误码。如果没有捕获到任何异常，则返回 `0` 表示成功。如果发生了 `EnvErrorAlreadySet` 异常，则返回 `-2`；如果发生了其他类型的异常，则调用  `TVMAPIHandleException()` 函数来处理异常并返回相应的值。\n",
    "\n",
    "此外，还定义了宏 `API_END_HANDLE_ERROR(Finalize)`，用于处理异常并在发生错误时执行清理操作。与 `API_END()` 类似，它也使用 `try` 和 `catch` 块来捕获异常，但在捕获到异常后，会先执行传入的 `Finalize` 参数所表示的清理操作，然后再调用 `TVMAPIHandleException()` 函数处理异常并返回相应的错误码。\n",
    "\n",
    "最后，定义了函数 `TVMAPIHandleException()`，用于处理异常。它接受 `std::exception` 类型的参数，并返回整数值，表示 API 执行后的返回值。"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
